#include "lista.h"

#include <iostream>

using namespace std;

void lista::add(int w)
{	
	element * e = new element(w);

	if(this->count == 0)
	{
		this->next = this->last = e;
	}
	else
	{
		e->prev = this->last;
		this->last->next = e;
		this->last = e;
	}

	this->count++;
}

void lista::del()
{
	if(this->count > 1)
	{
		this->last = this->last->prev;
		this->last->next = NULL;
		this->count--;
	}
	else if(this->count == 1)
	{
		this->last = this->next = NULL;
		this->count--;
	}
}

void lista::wypisz()
{
	element * tmp = this->next;

	for(int i=0; i<this->count; i++)
	{
		cout << tmp->wartosc << ", ";
		tmp = tmp->next;
	}
}

element * lista::find(int value)
{
	element * tmp = this->next;

	for(int i=0; i<this->count; i++)
	{
		if(tmp->wartosc == value)
			return tmp;

		tmp = tmp->next;
	}

	return NULL;
}

element * lista::getElementIfEqual(int position, int value)
{
	element * tmp = this->next;
	int licznik = 0;

	for(int i=0; i<position; i++)
		tmp = tmp->next;

	if(tmp->wartosc == value)
			return tmp;

	return NULL;
}

void lista::delPointer(element * e)
{
	element * tmp = this->next;

		for(; tmp != NULL ;)
		{
			if(tmp == e)
			{
				if(tmp->prev == NULL)
				{
					this->next = tmp->next;
					if(tmp->next != NULL)
					{
						tmp->next->prev = NULL;
					}
					else if(tmp->next == NULL)
					{
						this->last = NULL;					
					}

					this->count--;
				}
				else
				{
					if(tmp->next != NULL)
					{
						tmp->prev->next = tmp->next;
						tmp->next->prev = tmp->prev;
					}
					else if(tmp->next == NULL)
					{
						this->last = tmp->prev;
						tmp->prev->next = tmp->next;
					}

					this->count--;
				}

				//delete tmp;
				return;
			}

			tmp = tmp->next;
		}
}